package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理controller
 * @author Xiao
 * @date 2021/9/24
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * 分类管理-新增分类(菜品/套餐)
     * @return
     */
    @PostMapping
    public R<String> saveCategory(@RequestBody Category category) {
        log.info("新增分类: {}", category);
        categoryService.saveCategory(category);
        return R.success("添加成功！");
    }
    /**
     * 分类管理-分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> getCategoryByPage(@RequestParam Integer page, Integer pageSize) {
        log.info("分类管理分页查询 --> page:{}, pageSize:{}",page,pageSize);
        Page<Category> categoryPage = categoryService.getCategoryByPage(page, pageSize);
        return R.success(categoryPage);
    }
    /**
     * 分类管理-根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(@RequestParam("ids") Long id) {
        log.info("要删除的分类id：{}",id);
        //categoryService.removeCategoryById(id);
        categoryService.removeCategoryByIdPlus(id);
        return R.success("删除成功！");
    }

    /**
     * 分类管理-根据id查找分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Category> queryCategoryById(@PathVariable Long id) {
        log.info("id：{}", id);
        Category category = categoryService.queryCategoryById(id);
        return R.success(category);
    }

    /**
     * 分类管理-编辑分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> editCategory(@RequestBody Category category) {
        log.info("要修改的分类：{}", category.getName());
        categoryService.editCategory(category);
        return R.success("修改成功！");
    }

    /**
     * 菜品管理-根据条件获取分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> getCategoryList(Category category) {
        log.info("分类类型(1-菜品，2-套餐)：{}", category.getType());
        List<Category> categoryList = categoryService.getCategoryList(category);
        return R.success(categoryList);
    }
}
